/**
 * @Title: TimeUtil.java
 * @Package com.uroad.com.uroad.locmap.util
 * @Description: TODO(��һ�仰������ļ���ʲô)
 * @author oupy
 * @date 2013-8-22 ����6:42:17
 * @version V1.0
 */
package com.uroad.lib.util.data;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期事件处理工具类
 *
 */
@SuppressLint("SimpleDateFormat")
public class DateTimeUtil {

    /*
     * 判断是白天还是黑夜,day:6-18;night:
     */
    public static boolean isDay() {
        boolean ret = false;
        if (Calendar.getInstance().HOUR_OF_DAY > 6
                && Calendar.getInstance().HOUR_OF_DAY < 18) {
            ret = true;
        }
        return ret;
    }

    /*
     * 获取当前时间特定时间的间隔
     */
    public static String timeAgo(String timeStr) {
        Date date = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            date = format.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }

        long timeStamp = date.getTime();

        Date currentTime = new Date();
        long currentTimeStamp = currentTime.getTime();
        long seconds = (currentTimeStamp - timeStamp) / 1000;

        long minutes = Math.abs(seconds / 60);
        long hours = Math.abs(minutes / 60);
        long days = Math.abs(hours / 24);

        if (seconds <= 15) {
            return "刚刚";
        } else if (seconds < 60) {
            return seconds + "秒前";
        } else if (seconds < 120) {
            return "1分钟前";
        } else if (minutes < 60) {
            return minutes + "分钟前";
        } else if (minutes < 120) {
            return "一小时前";
        } else if (hours < 24) {
            return hours + "小时前";
        } else if (hours < 24 * 2) {
            return "昨天";
        } else if (days < 30) {
            return days + "天前";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
            String dateString = formatter.format(date);
            return dateString;
        }

    }

    /*
     * 获取当前时间特定时间的间隔
     */
    public static String timeAgo(String timeStr, String formatstr) {
        Date date = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatstr);
            date = format.parse(timeStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }

        long timeStamp = date.getTime();

        Date currentTime = new Date();
        long currentTimeStamp = currentTime.getTime();
        long seconds = (currentTimeStamp - timeStamp) / 1000;

        long minutes = Math.abs(seconds / 60);
        long hours = Math.abs(minutes / 60);
        long days = Math.abs(hours / 24);

        if (seconds <= 15) {
            return "刚刚";
        } else if (seconds < 60) {
            return seconds + "秒前";
        } else if (seconds < 120) {
            return "1分钟前";
        } else if (minutes < 60) {
            return minutes + "分钟前";
        } else if (minutes < 120) {
            return "一小时前";
        } else if (hours < 24) {
            return hours + "小时前";
        } else if (hours < 24 * 2) {
            return "一天前";
        } else if (days < 30) {
            return days + "天前";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
            String dateString = formatter.format(date);
            return dateString;
        }

    }

    /**
     * 获取前几个月的日期
     *
     * @param monthNum   前‘monthNum’个月的日期
     * @param dataFormat 日期格式（yyyyMM）
     */
    public static String[] getLastFewMonth(int monthNum, String dataFormat) {

        String[] lastFewMonth = new String[monthNum];
        try {
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat(dataFormat);
            cal.add(Calendar.MONTH,1);
//            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1); //逐次往前推1个月
            for (int i = 0; i < monthNum; i++) {
//                set方法有个巨坑，这里有一个bug，三月29日减去一个月，居然是三月1号！所以每次在减之前，需要将日改为1
//                cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1); //逐次往前推1个月
                cal.add(Calendar.MONTH,-1);
                lastFewMonth[i] = sdf.format(cal.getTime());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return lastFewMonth;
        }
        return lastFewMonth;
    }

    /**
     * 得到特定日期几天后的时间(负值为几天前日期)
     */
    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

    /**
     * 得到特定日期几个月后的时间(负值为几个月前日期)
     * **/
    public static Date getMonthAfter(Date date, int month) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.MONTH, now.get(Calendar.MONTH) + month);
        return now.getTime();
    }

    // String pTime = "2012-03-12";
    public static String getWeek(String pTime) {

        String Week = "星期";

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        try {

            c.setTime(format.parse(pTime));

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 1) {
            Week += "天";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 2) {
            Week += "一";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 3) {
            Week += "二";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 4) {
            Week += "三";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 5) {
            Week += "四";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 6) {
            Week += "五";
        }
        if (c.get(Calendar.DAY_OF_WEEK) == 7) {
            Week += "六";
        }

        return Week;
    }

    // 获取当前时间

    /**
     * patt 时间格式 （yyyy-MM-dd）
     *
     * @param patt
     * @return
     */
    public static String getCurrTime(String patt) {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat(patt);
        return sf.format(date);
    }

    /**
     * 获取一个月的天数
     * **/
    public static int GetDaysInMonth(int y, int m) {
        int d = 0;
        try {
            int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            if (2 == m) {
                d = (((0 == y % 4) && (0 != y % 100) || (0 == y % 400)) ? 29
                        : 28);
            } else {
                d = day[m - 1];

            }
        } catch (Exception e) {
        }
        return d;
    }

    /**
     * 转换日期格式，curFormat：原来日期格式，desFormat：要转换的日期格式
     * **/
    public static String getFommateDate(String dateStr, String curFormat,
                                        String desFormat) {
        if(TextUtils.isEmpty(dateStr)){
            return "";
        }
        String str = dateStr;
        SimpleDateFormat curformatter = new SimpleDateFormat(curFormat);
        SimpleDateFormat desformatter = new SimpleDateFormat(desFormat);
        Date date = null;
        try {
            date = curformatter.parse(dateStr);
            str = desformatter.format(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        }
        return str;
    }

    /**
     * 比较两个日期大小，返回：1,0，-1
     *
     * an int < 0 if this Date is less than the specified Date, 0 if they are
     * equal, and an int > 0 if this Date is greater
     * **/
    public static int compareDate(Date date1, Date date2) {
        if (date1 == null || date2 == null)
            return 0;
        return date1.compareTo(date2);
    }

    /**
     * 将日期转成固定格式的字符串
     * **/
    public static String dateToString(Date d, String formatter) {
        String str = "";
        try {
            SimpleDateFormat f = new SimpleDateFormat(formatter);
            str = f.format(d);
        } catch (Exception e) {
        }
        return str;
    }

    /**
     * 将日期字符串转换为date，需传入当前日期的格式
     * **/
    public static Date strToDate(String dateStr, String formatter) {
        Date date = null;
        try {
            SimpleDateFormat f = new SimpleDateFormat(formatter);
            date = f.parse(dateStr);
        } catch (Exception e) {
        }
        return date;
    }

    /**
     * 将毫秒数转换为00时00分00秒格式
     * **/
    public static String strToTime(long time) {
        String str = "";
        long seconds = time / 1000;
        int hour = (int) (seconds / 3600);
        seconds -= 3600 * hour;
        int minute = (int) seconds / 60;
        seconds -= 60 * minute;
        if (hour > 0) {
            str += hour + "小时";
        }
        if (minute > 0) {
            str += minute + "分钟";
        }
        if (seconds > 0) {
            str += seconds + "秒";
        }
        return str;
    }

    /**
     * 将毫秒数转换为00:00:00格式
     * **/
    public static String strToTime2(long time) {
        String str = "";
        long seconds = time / 1000;
        int hour = (int) (seconds / 3600);
        seconds -= 3600 * hour;
        int minute = (int) seconds / 60;
        seconds -= 60 * minute;
        if (hour >= 0) {
            str += hour + ":";
        }
        if (minute >= 0) {
            str += minute + ":";
        }
        if (seconds >= 0) {
            str += seconds + "";
        }
        return str;
    }

    /**
     * 计算时间差（date1-date2）
     * 返回格式：00天00时00分
     * **/
    public static String getDiffTime(String date1,String date2){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try
        {
            Date d1 = df.parse(date1);
            Date d2 = df.parse(date2);
            long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别

            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff-days*(1000 * 60 * 60 * 24))/(1000* 60 * 60);
            long minutes = (diff-days*(1000 * 60 * 60 * 24)-hours*(1000* 60 * 60))/(1000* 60);

            return  ""+days+"天"+hours+"小时"+minutes+"分";
        }
        catch (Exception e)
        {
        }

        return "";
    }

    /**
     * 计算时间差（date1-date2）
     * 返回格式：00时00分00秒
     * **/
    public static String getDiffTime1(String date1,String date2){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try
        {
            Date d1 = df.parse(date1);
            Date d2 = df.parse(date2);
            long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别

            long hours = diff/(1000* 60 * 60);
            long minutes = (diff-hours*(1000* 60 * 60))/(1000* 60);
            long seconds = (diff-hours*(1000* 60 * 60)-minutes*(1000*60))/1000;
            String hourStr=hours+"";
            String minuteStr=minutes+"";
            String secondStr=seconds+"";
            if(hours<10){
                hourStr="0"+hours;
            }

            if(minutes<10){
                minuteStr="0"+minutes;
            }

            if(seconds<10){
                secondStr="0"+seconds;
            }

            return  hourStr+":"+minuteStr+":"+secondStr+"";
        }
        catch (Exception e)
        {
        }

        return "";
    }

}
